package com.qianguo.buyer.order;

import android.content.Context;
import android.content.Intent;
import android.text.TextUtils;
import android.text.format.DateFormat;
import android.text.format.DateUtils;
import android.view.View;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;

import com.eva.androidlibrary.api.HttpRequestCallback;
import com.qianguo.buyer.R;
import com.qianguo.buyer.base.BaseTitleActivity;
import com.qianguo.buyer.constant.BuyerKeys;
import com.qianguo.buyer.entity.BuyerOrder;

import org.androidannotations.annotations.Background;
import org.androidannotations.annotations.Click;
import org.androidannotations.annotations.EActivity;
import org.androidannotations.annotations.Extra;
import org.androidannotations.annotations.UiThread;
import org.androidannotations.annotations.ViewById;

/**
 * 买家订单详情
 *
 * @author eva
 * @date 16/6/29
 */
@EActivity(R.layout.activity_buyer_order_detail)
public class BuyerOrderDetailActivity extends BaseTitleActivity {

    @ViewById(R.id.buyer_order_detail_order_status_text_view)
    TextView mTvOrderStatusIntro;

    @ViewById(R.id.buyer_order_detail_shop_name_text_view)
    TextView mTvShopName;

    @ViewById(R.id.buyer_order_detail_shop_address_text_view)
    TextView mTvShopAddress;

    @ViewById(R.id.buyer_order_detail_receiver_info_text_view)
    TextView mTvReceiverInfo;

    @ViewById(R.id.buyer_order_detail_receiver_address_text_view)
    TextView mTvReceiverAddress;

    @ViewById(R.id.buyer_order_detail_receiver_intro_text_view)
    TextView mTvReceiverRemark;

    @ViewById(R.id.buyer_order_detail_delivery_time_text_view)
    TextView mTvDeliveryTime;

    @ViewById(R.id.buyer_order_detail_order_time_text_view)
    TextView mTvOrderTime;

    @ViewById(R.id.buyer_order_detail_price_text_view)
    TextView mTvOrderPrice;

    @ViewById(R.id.buyer_order_detail_delivery_price_text_view)
    TextView mTvDeliveryPrice;

    @ViewById(R.id.buyer_order_detail_product_list_view)
    ListView mProductListView;

    @ViewById(R.id.buyer_order_detail_action_button)
    Button mBtnOrderAction;

    @ViewById(R.id.buyer_order_detail_action_layout)
    LinearLayout mOrderActionLayout;

    @ViewById(R.id.buyer_order_detail_receiver_auth_code_text_view)
    TextView mTvReceiverAuthCode;

    @Extra(BuyerKeys.EXTRA_ORDER_SN)
    String mOrderSN;

    public static void jumpToOrderDetail(Context context, String orderSN) {
        Intent intent = new Intent();
        intent.setClass(context, BuyerOrderDetailActivity_.class);
        intent.putExtra(BuyerKeys.EXTRA_ORDER_SN, orderSN);
        context.startActivity(intent);
    }

    @Override
    protected String getToolbarTitle() {
        return getString(R.string.buyer_order_detail_title);
    }

    @Override
    protected void initializeUI() {
        if (TextUtils.isEmpty(mOrderSN)) {
            showSnackBar(this, "订单号为空...");
        } else {
            getOrderDetail();
        }
    }

    @Background
    void getOrderDetail() {
        mApp.getApi().getOrderDetail(mApp.getUser().getToken(), mOrderSN, new HttpRequestCallback<BuyerOrder>() {
            @Override
            public void onSuccess(int code, String msg, BuyerOrder data) {
                refreshOrderDetail(data);
            }

            @Override
            public void onFailure(int code, String msg) {
                showSnackBar(BuyerOrderDetailActivity.this, msg);
            }
        });
    }

    @UiThread
    void refreshOrderDetail(BuyerOrder order) {
        mTvOrderStatusIntro.setText(order.getTagIntro());
        mTvShopName.setText(getString(R.string.buyer_order_detail_shop_name, order.getStoreName()));
        mTvShopAddress.setText(getString(R.string.buyer_order_detail_shop_address, order.getStoreAddress()));
        mTvReceiverInfo.setText(getString(R.string.buyer_order_detail_receiver_info, order.getReceiverName(), order.getReceiverPhone()));
        mTvReceiverAddress.setText(getString(R.string.buyer_order_detail_receiver_address, order.getReceiverAddress()));
        mTvDeliveryTime.setText(getString(R.string.buyer_order_detail_delivery_time, order.getDeliveryTime()));
        // TODO: 16/6/29 时间转换
        mTvOrderTime.setText(getString(R.string.buyer_order_detail_order_time, DateFormat.format("yyyy-MM-dd HH:mm:ss", order.getOrderTime() / 1000)));
        mTvReceiverRemark.setText(getString(R.string.buyer_order_detail_order_remark, order.getReceiverIntro()));
        mTvOrderPrice.setText(getString(R.string.buyer_order_detail_price, Float.valueOf(order.getPrice())));
        mTvDeliveryPrice.setText(getString(R.string.buyer_order_detail_price, Float.valueOf(order.getDeliveryPrice())));
        mTvReceiverAuthCode.setText(order.getReceiverAuthCode());

        switch(order.getPayCode()) {
            case BuyerOrder.TYPE_CANCEL:
                mBtnOrderAction.setText(getString(R.string.buyer_order_detail_reorder));
                mBtnOrderAction.setOnClickListener(mReOrderClickListener);
                break;
            case BuyerOrder.TYPE_FINISH:
                mOrderActionLayout.setVisibility(View.GONE);
                break;
            case BuyerOrder.TYPE_DOING:
                mBtnOrderAction.setOnClickListener(mPayClickListener);
                mBtnOrderAction.setEnabled(order.canPay());
                mBtnOrderAction.setText(getString(R.string.buyer_order_detail_pay));
                break;
        }
    }

    private View.OnClickListener mPayClickListener = new View.OnClickListener() {
        @Override
        public void onClick(View v) {

        }
    };

    private View.OnClickListener mReOrderClickListener = new View.OnClickListener() {
        @Override
        public void onClick(View v) {

        }
    };

}
